﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Shop_Manager
{
    public partial class TonKho : Form
    {
        public TonKho()
        {
            InitializeComponent();
        }


        private void XuatKho_Load(object sender, EventArgs e)
        {
            txtTenHang.Text = "";
            txtMaMH.Text = "";
            grdKq.DataSource = null;
            grdKq.Rows.Clear();
            grdKq.Refresh();
            loadAllMatHangToDataGrid();
            
        }

        private void loadAllMatHangToDataGrid()
        { 
            //Lay ra tat ca cac mat hang co trong kho
            DataSet dsAllQuay = DataConn.procedureSelectCommand("selectAllQuay");
            if (dsAllQuay != null && dsAllQuay.Tables[0].Rows.Count > 0)
            {
                grdKq.ColumnCount = 3 + dsAllQuay.Tables[0].Rows.Count;
                grdKq.Columns[0].HeaderText = "Mã Mặt Hàng";
                grdKq.Columns[0].Name = "MaMH";
                grdKq.Columns[1].HeaderText = "Tên Mặt Hàng";
                grdKq.Columns[1].Name = "TenMH";
                grdKq.Columns[2].HeaderText = "Số lượng trong kho";
                grdKq.Columns[2].Name = "SoLuongKho";
                for (int i = 0; i < dsAllQuay.Tables[0].Rows.Count; i++)
                {
                    //them cot ung voi moi quay vao datagridview
                    grdKq.Columns[3 + i].HeaderText = "Số lượng trong " + dsAllQuay.Tables[0].Rows[i]["TenQuay"].ToString();
                    grdKq.Columns[3 + i].Name = dsAllQuay.Tables[0].Rows[i]["MaQuay"].ToString();
                }

                DataSet dsAllMH = DataConn.procedureSelectCommand("selectAll_MatHang");
                if (dsAllMH != null && dsAllMH.Tables[0].Rows.Count > 0)
                {
                    int count = 3;
                    count = count + dsAllQuay.Tables[0].Rows.Count;
                    foreach (DataRow rowMH in dsAllMH.Tables[0].Rows)
                    {
                        string[] collectionMatHang;

                        collectionMatHang = new string[count];
                        collectionMatHang[0] = rowMH["Mã hàng"].ToString();
                        collectionMatHang[1] = rowMH["Tên mặt hàng"].ToString();
                        collectionMatHang[2] = rowMH["Số lượng trong kho"].ToString();

                        //lay ra so luong cua tung MH trong tung quay
                        foreach (DataRow rowQuay in dsAllQuay.Tables[0].Rows)
                        {
                            //lay ra so luong MH chua trong moi quay
                            SqlParameter[] parametersQuay = 
                    { 
                        new SqlParameter("@maQuay", SqlDbType.NVarChar,50),
                        new SqlParameter("@maMH", SqlDbType.NVarChar,50)
                    };
                            parametersQuay[0].Value = rowQuay["MaQuay"].ToString();
                            parametersQuay[1].Value = rowMH["Mã hàng"].ToString();
                            DataSet dsSoLuongMH = DataConn.procedureSelectCommandHaveParameters("selectSoLuongSPQuayByMaMHandMaQuay", parametersQuay);
                            if (dsSoLuongMH != null && dsSoLuongMH.Tables[0].Rows.Count > 0)
                            {
                                //duyet tat ca cac cot trong grid de tim ra ma quay 
                                for (int i = 0; i < grdKq.Columns.Count; i++)
                                {
                                    if (grdKq.Columns[i].Name.Equals(rowQuay["MaQuay"].ToString()))
                                    {
                                        //them so luong cua 1 MH o moi quay vao datagridview
                                        collectionMatHang[i] = dsSoLuongMH.Tables[0].Rows[0][0].ToString();
                                    }
                                }
                            }

                        }
                        //add string[] vao datagrid
                        grdKq.Rows.Add(collectionMatHang);
                        grdKq.Update();
                    }
                }

                
            }
            


        }

        

        private void loadMatHangToGridByMaMH(string maMH)
        {
            DataSet dsAllQuay = DataConn.procedureSelectCommand("selectAllQuay");
            if (dsAllQuay != null && dsAllQuay.Tables[0].Rows.Count > 0)
            {
                //Create column vao grid
                grdKq.ColumnCount = 3 + dsAllQuay.Tables[0].Rows.Count;
                grdKq.Columns[0].HeaderText = "Mã Mặt Hàng";
                grdKq.Columns[0].Name = "MaMH";
                grdKq.Columns[1].HeaderText = "Tên Mặt Hàng";
                grdKq.Columns[1].Name = "TenMH";
                grdKq.Columns[2].HeaderText = "Số lượng trong kho";
                grdKq.Columns[2].Name = "SoLuongKho";
                for (int i = 0; i < dsAllQuay.Tables[0].Rows.Count; i++)
                {
                    //them cot ung voi moi quay vao datagridview
                    grdKq.Columns[3 + i].HeaderText = "Số lượng trong " + dsAllQuay.Tables[0].Rows[i]["TenQuay"].ToString();
                    grdKq.Columns[3 + i].Name = dsAllQuay.Tables[0].Rows[i]["MaQuay"].ToString();
                }

                //lay mat hang theo ma mat hang
                SqlParameter[] parametersMH = 
                { 
                    new SqlParameter("@mamh", SqlDbType.NVarChar,50)
                };
                parametersMH[0].Value = maMH;
                DataSet dsMH = DataConn.procedureSelectCommandHaveParameters("selectMatHangByMaMH", parametersMH);
                if (dsMH != null && dsMH.Tables[0].Rows.Count > 0)
                {
                    txtTenHang.Text = dsMH.Tables[0].Rows[0]["Tên mặt hàng"].ToString();
                    string[] collectionMatHang;
                    int count = 3;
                    count = count + dsAllQuay.Tables[0].Rows.Count;
                    collectionMatHang = new string[count];
                    collectionMatHang[0] = dsMH.Tables[0].Rows[0]["Mã hàng"].ToString();
                    collectionMatHang[1] = dsMH.Tables[0].Rows[0]["Tên mặt hàng"].ToString();
                    collectionMatHang[2] = dsMH.Tables[0].Rows[0]["Số lượng trong kho"].ToString();

                    foreach (DataRow rowQuay in dsAllQuay.Tables[0].Rows)
                    {
                        //lay ra so luong MH chua trong moi quay
                        SqlParameter[] parametersQuay = 
                        { 
                            new SqlParameter("@maQuay", SqlDbType.NVarChar,50),
                            new SqlParameter("@maMH", SqlDbType.NVarChar,50)
                        };
                        parametersQuay[0].Value = rowQuay["MaQuay"].ToString();
                        parametersQuay[1].Value = maMH;
                        DataSet dsSoLuongMH = DataConn.procedureSelectCommandHaveParameters("selectSoLuongSPQuayByMaMHandMaQuay", parametersQuay);
                        if (dsSoLuongMH != null && dsSoLuongMH.Tables[0].Rows.Count > 0)
                        {
                            //duyet tat ca cac cot trong grid de tim ra ma quay 
                            for (int i = 0; i < grdKq.Columns.Count; i++)
                            {
                                if (grdKq.Columns[i].Name.Equals(rowQuay["MaQuay"].ToString()))
                                {
                                    //them so luong cua 1 MH o moi quay vao datagridview
                                    collectionMatHang[i] = dsSoLuongMH.Tables[0].Rows[0][0].ToString();
                                }
                            }
                        }
                    }
                    grdKq.Rows.Add(collectionMatHang);
                    grdKq.Refresh();
                    //grdKq.Update();
                }
                else
                {
                    MessageBox.Show("Mặt Hàng này không có trong kho!","");
                    txtTenHang.Text = "";
                    grdKq.DataSource = null;
                    grdKq.Rows.Clear();
                    grdKq.Refresh();
                    loadAllMatHangToDataGrid();
                    txtMaMH.Focus();
                }
                
            }
        }

        private void txtMaMH_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtMaMH.Text))
            {
                txtTenHang.Text = "";
                grdKq.DataSource = null;
                grdKq.Rows.Clear();
                grdKq.Refresh();
                loadAllMatHangToDataGrid();
            }
        }

        private void txtMaMH_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (String.IsNullOrEmpty(txtMaMH.Text))
                {
                    txtMaMH_TextChanged(sender, e);
                }
                else
                {
                    grdKq.DataSource = null;
                    grdKq.Rows.Clear();
                    grdKq.Refresh();
                    loadMatHangToGridByMaMH(txtMaMH.Text);
                }

            }
        }
            
    }
}